package com.nbgroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbgroup.mapper.TransferMapper;
import com.nbgroup.pojo.Dept;
import com.nbgroup.pojo.common.PageResult;
import com.nbgroup.pojo.User;
import com.nbgroup.pojo.common.User_transfer;
import com.nbgroup.pojo.dto.PageTransferDto;
import com.nbgroup.pojo.vo.*;
import com.nbgroup.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    private TransferMapper transferMapper;

    @Override
    public PageResult list(PageTransferDto dto) {
        dto.check();
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<User_transfer> p = transferMapper.list(dto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(p.getTotal());
        pageResult.setRows(p.getResult());
        pageResult.setParams(null);
        pageResult.setMsg("查询成功");
        pageResult.setCode(200);

        return pageResult;
    }

    @Override
    public ArrayList<CompanyVo> select() {
        ArrayList<CompanyVo> list1 = transferMapper.select();
        List<Integer> companySonIdList = list1.stream().map(l -> l.getCompanySonId()).collect(Collectors.toList());
        //通过外键id查询子公司集合
        ArrayList<CompanySonVo> list2 = transferMapper.selectCompanySon(companySonIdList);
        //封装最外层的子公司
        for (CompanyVo companyVo : list1) {
            List<CompanySonVo> companySon = list2.stream().filter(l2 -> l2.getId().equals(companyVo.getCompanySonId())).collect(Collectors.toList());
            companyVo.setChildren(companySon);
        }

        //得到部门外键id集合
        List<Integer> departmentIdList = list2.stream().map(l2 -> l2.getDepartmentId()).collect(Collectors.toList());
        //通过外键id查询部门
        ArrayList<DepartmentVo> list3 = transferMapper.selectDepartment(departmentIdList);
        //封装第二层公司的子公司
        for (CompanySonVo companySonVo : list2) {
            List<DepartmentVo> department = list3.stream().filter(l3 -> l3.getId().equals(companySonVo.getDepartmentId())).collect(Collectors.toList());
            companySonVo.setChildren(department);
        }

        //得到子部门id集合
        List<Integer> departmentSonIdList = list3.stream().map(l3 -> l3.getDepartmentSonId()).collect(Collectors.toList());
        //通过外键id查询子部门
        ArrayList<DepartmentSonVo> list4 = transferMapper.selectDepartmentSon(departmentSonIdList);
        //封装第三层部门的子部门
        for (DepartmentVo departmentVo : list3) {
            List<DepartmentSonVo> departmentSon = list4.stream().filter(l4 -> l4.getId().equals(departmentVo.getDepartmentSonId())).collect(Collectors.toList());
            departmentVo.setChildren(departmentSon);
        }

        return list1;

    }

    @Override
    public List<User> userList(Integer deptId) {
        List<User> users = transferMapper.queryCom(deptId);
        if (CollectionUtils.isEmpty(users)){
            return null;
        }
        List<Dept> depts = transferMapper.deptList(Arrays.asList(deptId));
        for (User user : users) {
            List<Dept> collect = depts.stream().filter(l -> l.getDeptId().equals(user.getDeptId())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collect)){
                user.setDept(collect.get(0));
            }
        }
        /*List<User> list = transferMapper.userList(deptId);

        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        List<Integer> deptIdList = list.stream().map(l -> l.getDeptId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(deptIdList)){
            return list;
        }
        List<Dept> list2 = transferMapper.deptList(deptIdList);
        for (User user : list) {
            List<Dept> dept = list2.stream().filter(l2 -> l2.getDeptId().equals(user.getDeptId())).collect(Collectors.toList());
            user.setDept(dept);
        }

        List<Integer> usersIdList = list2.stream().map(l2 -> l2.getUsersId()).collect(Collectors.toList());
        List<User_transfer> list3 = transferMapper.usersList(usersIdList);
        for (Dept dept : list2) {
            List<User_transfer> usersList = list3.stream().filter(l3 -> l3.getUserId().equals(dept.getUsersId())).collect(Collectors.toList());
            dept.setUsers(usersList);
        }*/

        return users;
    }

    @Override
    public TransferDataVo assigment(Integer status, Integer deptId, Integer id) {
        TransferDataVo transferDataVo = new TransferDataVo();
        //transferMapper.assigment(status,deptId,id);
        transferDataVo.setFlag(false);
        transferDataVo.setMsg("转换失败");
        return transferDataVo;
    }


}
